﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DAL;
using BLL;
using HCSExtensions;
using Dynamics.Enums;
using Dynamics.Models;
using System.Diagnostics;

public class OpportunityParser
{
    public List<Site> Sites { get; set; }
    public List<Product> Products { get; set; }
    public List<Keyword> Keywords { get; set; }
    //public const int GrupfoniSiteID = 2;
    //public const int BirlikteAlalimSiteID = 2;

    public void ParseData()
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        Sites = new SiteProvider().GetSites(true);
        Keywords = new KeywordProvider().GetKeywords();
        Products = new List<Product>();

        if (Sites.Count == 0 || Keywords.Count == 0)
        {
            return;
        }

        foreach (Site site in Sites)
        {
            ParseSite(site);
        }

        if (Products.Count == 0)
        {
            return;
        }

        stopWatch.Reset();

        stopWatch.Start();
        ProductProvider provider = new ProductProvider();
        TransactionEvent transaction = new TransactionEvent();
        foreach (Product product in Products)
        {
            if (!CheckKeyword(product))
            {
                continue;
            }
            transaction = provider.SaveProduct(product);
            if (!transaction.IsSuccess)
            {
                LogProvider.SaveLog(LogType.ProductSaveError, (DataSource)product.SiteID, product.SerializeObject(), transaction.FullErrorMessage);
            }
        }

        stopWatch.Reset();



    }

    private bool CheckKeyword(Product product)
    {
        bool exists = false;

        foreach (Keyword keyword in Keywords)
        {
            if (product.Heading.ToLower().Contains(keyword.Name.ToLower()))
            {
                exists = true;
                break;
            }
            //if (product.Description.ToLower().Contains(keyword.Name.ToLower()))
            //{
            //    exists = true;
            //    break;
            //}
            //if (product.Detail.ToLower().Contains(keyword.Name.ToLower()))
            //{
            //    exists = true;
            //    break;
            //}
        }

        return exists;
    }

    private void ParseSite(Site site)
    {
        if (site == null || string.IsNullOrEmpty(site.XMLUrl))
        {
            return;
        }

        TransactionEvent transaction = DataHelper.GetData(site.XMLUrl);
        if (!transaction.IsSuccess)
        {
            LogProvider.SaveLog(LogType.DataReceiveError, transaction.FullErrorMessage);
            return;
        }

        string result = transaction.ReturnObject.ToString();
        transaction = DataHelper.ParseProductsFromXML(result);
        if (!transaction.IsSuccess)
        {
            LogProvider.SaveLog(LogType.DataParseError, (DataSource)site.ID, transaction.FullErrorMessage);
            return;
        }

        ProductParseItem parseItem = (ProductParseItem)transaction.ReturnObject;
        if (parseItem.LogItems.Count > 0)
        {
            foreach (ProductErrorLogItem logItem in parseItem.LogItems)
            {
                LogProvider.SaveLog(LogType.DataParseError, (DataSource)site.ID, logItem.SerializeObject());
            }
        }

        parseItem.Products.ForEach(t => t.SiteID = site.ID);
        Products.AddRange(parseItem.Products);
    }


}
